package com.zhejiangzhengyuan.municipal_security.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.dto.RoleDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.RoleVo;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.ActionService;
import com.zhejiangzhengyuan.municipal_common.service.RoleService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/** @author lin_ju Create on 2019/11/26 */
@Api(tags = "权限信息")
@RestController
@CrossOrigin
@RequestMapping("/security/role")
public class RoleController {
  @Autowired private RoleService roleService;
  @Autowired private ActionService actionService;

  /* @RequestMapping(method = RequestMethod.GET)
  public List<Role> findAll() {
      return roleService.list();
  }*/

  /*@RequestMapping(value = "/{name}",method = RequestMethod.GET)
  public Role findByName(@PathVariable String name){
      QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
      QueryWrapper<Role> roleName = roleQueryWrapper.eq("role_name", name);
      return  roleService.getOne(roleName);
  }*/

  @ApiOperation(value = "设置当前页和每页显示条数", notes = "根据当前页和每页显示条数分页查询")
  @GetMapping("/selectRolePage")
  public ResponseVo selectRolePage(@ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit) {
    // pageNo默认为1；size默认为15
    page = page == null || page <= 0 ? 1 : page;
    limit = limit == null || limit <= 0 ? 11 : limit;
    Page<RoleVo> roleIPage = new Page<>(page, limit);
    Page<RoleVo> roleVoPage = roleService.selectRolePage(roleIPage);

    return new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, roleVoPage);
  }

  @ApiOperation(value = "添加角色")
  @PostMapping("/insertRole")
  public ResponseVo insertRole(@RequestBody RoleDto roleDto) {
    int i = roleService.insertRole(roleDto);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation(value = "编辑角色")
  @PutMapping("/updateRole")
  public ResponseVo updateRole(@RequestBody RoleDto roleDto) {
    if (Objects.isNull(roleDto.getIdList())) {
      return new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, "请添加相应的权限");
    }
    return roleService.updateRole(roleDto) > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation(value = "删除角色")
  @DeleteMapping("/deleteRole")
  public ResponseVo deleteRole(Long id) {
    int i = roleService.deleteRole(id);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("查询API")
  @GetMapping("/selectActionAll")
  public ResponseVo selectActionAll() {
    return actionService.getAllActionVo();
  }
}
